/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * Product.java
 *
 * Created on Oct 30, 2013, 8:39:18 PM
 */
package sims.gui;

import java.util.List;
import java.util.Vector;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;
import sims.dao.CategoryDao;
import sims.dao.MeasureDao;
import sims.dao.ProductDao;
import sims.dao.SupplierDao;
import sims.entity.TbCategory;
import sims.entity.TbEmployee;
import sims.entity.TbMeasure;
import sims.entity.TbProduct;
import sims.entity.TbSupplier;
import sims.library.Validate;

/**
 *
 * @author NINH
 */
public class Product extends javax.swing.JPanel {

    ProductDao productDao;
    CategoryDao cateDao;
    SupplierDao supDao;
    MeasureDao measureDao;
    Validate valid;
    Vector vCateId;
    Vector vSupId;
    Vector vMeasureId;

    /** Creates new form Product */
    public Product() {
        initComponents();
        productDao = new ProductDao();
        cateDao = new CategoryDao();
        supDao = new SupplierDao();
        measureDao = new MeasureDao();
        valid = new Validate();
        loadData();
        Reset();
        loadComboboxId();
        disableTextbox();
    }

    public void loadData() {
        tblProduct.setModel(DisplayAll());
    }

    public void loadCombobox() {
        cboCategory.setModel(cateDao.combobox());
        cboMeasure.setModel(measureDao.combobox());
        cboSupplier.setModel(supDao.combobox());
    }

    public void loadComboboxId() {
        vCateId = cateDao.comboboxId();
        vSupId = supDao.comboboxId();
        vMeasureId = measureDao.comboboxId();
    }

    public DefaultTableModel DisplayAll() {
        Vector vtable = new Vector();
        Vector vrow = new Vector();
        Vector vheader = new Vector();
        List<TbProduct> products = productDao.getAll(TbProduct.class);

        vheader.add("Id");
        vheader.add("Name");
        vheader.add("Price");
        vheader.add("Quantity");
        vheader.add("Category");
        vheader.add("Supplier");
        vheader.add("Measure");

        for (TbProduct p : products) {
            vrow.add(p.getPid());
            vrow.add(p.getPname());
            vrow.add(p.getPprice());
            vrow.add(p.getPquantity());
            vrow.add(p.getTbCategory().getCateName());
            vrow.add(p.getTbSupplier().getSupName());
            vrow.add(p.getTbMeasure().getMeasureName());
            vrow.add(p.getStatus() ? "Enable" : "Disable");
            vtable.add(vrow);
            vrow = new Vector();
        }
        return new DefaultTableModel(vtable, vheader);
    }

    public void Reset() {
        txtId.setText("");
        txtQuantity.setText("");
        txtName.setText("");
        txtPrice.setText("");
        txtDetail.setText("");
        cboCategory.setModel(new DefaultComboBoxModel());
        cboMeasure.setModel(new DefaultComboBoxModel());
        cboSupplier.setModel(new DefaultComboBoxModel());
    }

    public void binding() {
        int row = tblProduct.getSelectedRow();
        int id = Integer.parseInt(tblProduct.getValueAt(row, 0).toString());
        TbProduct p = (TbProduct) productDao.getById(TbProduct.class, id);

        txtId.setText(id + "");
        txtName.setText(p.getPname());
        txtDetail.setText(p.getPdetail());
        txtPrice.setText(p.getPprice().toString());
        txtQuantity.setText(p.getPquantity().toString());

        loadCombobox();
        enableTextbox();
        cboCategory.setSelectedItem(p.getTbCategory().getCateName());
        cboSupplier.setSelectedItem(p.getTbSupplier().getSupName());
        cboMeasure.setSelectedItem(p.getTbMeasure().getMeasureName());
    }

    public void disableTextbox() {
        txtDetail.setEnabled(false);
        txtName.setEnabled(false);
        txtPrice.setEnabled(false);
        txtQuantity.setEnabled(false);
        cboCategory.setEnabled(false);
        cboMeasure.setEnabled(false);
        cboSupplier.setEnabled(false);
    }

    public void enableTextbox() {
        txtDetail.setEnabled(true);
        txtName.setEnabled(true);
        txtPrice.setEnabled(true);
        txtQuantity.setEnabled(true);
        cboCategory.setEnabled(true);
        cboMeasure.setEnabled(true);
        cboSupplier.setEnabled(true);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        tblProduct = new javax.swing.JTable();
        jPanel1 = new javax.swing.JPanel();
        lblSupplier = new javax.swing.JLabel();
        lblCategory = new javax.swing.JLabel();
        lblMeasure = new javax.swing.JLabel();
        lblDescription = new javax.swing.JLabel();
        txtDetail = new javax.swing.JTextField();
        jLabel24 = new javax.swing.JLabel();
        jLabel22 = new javax.swing.JLabel();
        jLabel20 = new javax.swing.JLabel();
        jLabel18 = new javax.swing.JLabel();
        lblName = new javax.swing.JLabel();
        lblPrice = new javax.swing.JLabel();
        lblQuantity = new javax.swing.JLabel();
        txtQuantity = new javax.swing.JTextField();
        txtPrice = new javax.swing.JTextField();
        txtName = new javax.swing.JTextField();
        jLabel7 = new javax.swing.JLabel();
        jLabel12 = new javax.swing.JLabel();
        jLabel14 = new javax.swing.JLabel();
        btnAdd = new javax.swing.JButton();
        btnUpdate = new javax.swing.JButton();
        btnDelete = new javax.swing.JButton();
        cboSupplier = new javax.swing.JComboBox();
        cboMeasure = new javax.swing.JComboBox();
        cboCategory = new javax.swing.JComboBox();
        txtId = new javax.swing.JTextField();
        lblId = new javax.swing.JLabel();
        jLabel8 = new javax.swing.JLabel();

        setBackground(new java.awt.Color(255, 255, 255));
        setBorder(javax.swing.BorderFactory.createTitledBorder("Product manager"));
        setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        tblProduct.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        jScrollPane1.setViewportView(tblProduct);

        add(jScrollPane1, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 20, 1030, 250));

        jPanel1.setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        lblSupplier.setFont(new java.awt.Font("Tahoma", 2, 11));
        lblSupplier.setForeground(new java.awt.Color(255, 0, 0));
        jPanel1.add(lblSupplier, new org.netbeans.lib.awtextra.AbsoluteConstraints(790, 100, 160, 20));

        lblCategory.setFont(new java.awt.Font("Tahoma", 2, 11));
        lblCategory.setForeground(new java.awt.Color(255, 0, 0));
        jPanel1.add(lblCategory, new org.netbeans.lib.awtextra.AbsoluteConstraints(790, 70, 160, 20));

        lblMeasure.setFont(new java.awt.Font("Tahoma", 2, 11));
        lblMeasure.setForeground(new java.awt.Color(255, 0, 0));
        jPanel1.add(lblMeasure, new org.netbeans.lib.awtextra.AbsoluteConstraints(790, 40, 160, 20));

        lblDescription.setFont(new java.awt.Font("Tahoma", 2, 11));
        lblDescription.setForeground(new java.awt.Color(255, 0, 0));
        jPanel1.add(lblDescription, new org.netbeans.lib.awtextra.AbsoluteConstraints(320, 70, 160, 20));
        jPanel1.add(txtDetail, new org.netbeans.lib.awtextra.AbsoluteConstraints(90, 70, 220, -1));

        jLabel24.setFont(new java.awt.Font("Tahoma", 1, 11));
        jLabel24.setText("Supplier: ");
        jPanel1.add(jLabel24, new org.netbeans.lib.awtextra.AbsoluteConstraints(490, 100, 70, 20));

        jLabel22.setFont(new java.awt.Font("Tahoma", 1, 11));
        jLabel22.setText("Category: ");
        jPanel1.add(jLabel22, new org.netbeans.lib.awtextra.AbsoluteConstraints(490, 70, 70, 20));

        jLabel20.setFont(new java.awt.Font("Tahoma", 1, 11));
        jLabel20.setText("Measure: ");
        jPanel1.add(jLabel20, new org.netbeans.lib.awtextra.AbsoluteConstraints(490, 40, 70, 20));

        jLabel18.setFont(new java.awt.Font("Tahoma", 1, 11));
        jLabel18.setText("Detail: ");
        jPanel1.add(jLabel18, new org.netbeans.lib.awtextra.AbsoluteConstraints(20, 70, 70, 20));

        lblName.setFont(new java.awt.Font("Tahoma", 2, 11));
        lblName.setForeground(new java.awt.Color(255, 0, 0));
        jPanel1.add(lblName, new org.netbeans.lib.awtextra.AbsoluteConstraints(320, 40, 160, 20));

        lblPrice.setFont(new java.awt.Font("Tahoma", 2, 11));
        lblPrice.setForeground(new java.awt.Color(255, 0, 0));
        jPanel1.add(lblPrice, new org.netbeans.lib.awtextra.AbsoluteConstraints(320, 100, 160, 20));

        lblQuantity.setFont(new java.awt.Font("Tahoma", 2, 11));
        lblQuantity.setForeground(new java.awt.Color(255, 0, 0));
        jPanel1.add(lblQuantity, new org.netbeans.lib.awtextra.AbsoluteConstraints(790, 10, 160, 20));
        jPanel1.add(txtQuantity, new org.netbeans.lib.awtextra.AbsoluteConstraints(560, 10, 220, -1));
        jPanel1.add(txtPrice, new org.netbeans.lib.awtextra.AbsoluteConstraints(90, 100, 220, -1));
        jPanel1.add(txtName, new org.netbeans.lib.awtextra.AbsoluteConstraints(90, 40, 220, -1));

        jLabel7.setFont(new java.awt.Font("Tahoma", 1, 11));
        jLabel7.setText("Name: ");
        jPanel1.add(jLabel7, new org.netbeans.lib.awtextra.AbsoluteConstraints(20, 40, 70, 20));

        jLabel12.setFont(new java.awt.Font("Tahoma", 1, 11));
        jLabel12.setText("Price: ");
        jPanel1.add(jLabel12, new org.netbeans.lib.awtextra.AbsoluteConstraints(20, 100, 70, 20));

        jLabel14.setFont(new java.awt.Font("Tahoma", 1, 11));
        jLabel14.setText("Quantity: ");
        jPanel1.add(jLabel14, new org.netbeans.lib.awtextra.AbsoluteConstraints(490, 10, 70, 20));

        btnAdd.setIcon(new javax.swing.ImageIcon(getClass().getResource("/sims/images/add.png"))); // NOI18N
        btnAdd.setText("Add");
        btnAdd.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAddActionPerformed(evt);
            }
        });
        jPanel1.add(btnAdd, new org.netbeans.lib.awtextra.AbsoluteConstraints(320, 150, -1, 24));

        btnUpdate.setIcon(new javax.swing.ImageIcon(getClass().getResource("/sims/images/edit.png"))); // NOI18N
        btnUpdate.setText("Edit");
        btnUpdate.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnUpdateActionPerformed(evt);
            }
        });
        jPanel1.add(btnUpdate, new org.netbeans.lib.awtextra.AbsoluteConstraints(420, 150, -1, 24));

        btnDelete.setIcon(new javax.swing.ImageIcon(getClass().getResource("/sims/images/delete.png"))); // NOI18N
        btnDelete.setText("Delete");
        btnDelete.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnDeleteActionPerformed(evt);
            }
        });
        jPanel1.add(btnDelete, new org.netbeans.lib.awtextra.AbsoluteConstraints(520, 150, -1, 24));

        cboSupplier.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
        jPanel1.add(cboSupplier, new org.netbeans.lib.awtextra.AbsoluteConstraints(560, 100, 220, -1));

        cboMeasure.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
        jPanel1.add(cboMeasure, new org.netbeans.lib.awtextra.AbsoluteConstraints(560, 40, 220, -1));

        cboCategory.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
        jPanel1.add(cboCategory, new org.netbeans.lib.awtextra.AbsoluteConstraints(560, 70, 220, -1));

        txtId.setEnabled(false);
        jPanel1.add(txtId, new org.netbeans.lib.awtextra.AbsoluteConstraints(90, 10, 220, -1));

        lblId.setFont(new java.awt.Font("Tahoma", 2, 11));
        lblId.setForeground(new java.awt.Color(255, 0, 0));
        jPanel1.add(lblId, new org.netbeans.lib.awtextra.AbsoluteConstraints(320, 10, 160, 20));

        jLabel8.setFont(new java.awt.Font("Tahoma", 1, 11));
        jLabel8.setText("Id: ");
        jPanel1.add(jLabel8, new org.netbeans.lib.awtextra.AbsoluteConstraints(20, 10, 70, 20));

        add(jPanel1, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 280, 1030, 260));
    }// </editor-fold>//GEN-END:initComponents

    private void btnAddActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAddActionPerformed
        // TODO add your handling code here:
        if (btnAdd.getText().equalsIgnoreCase("add")) {
            enableTextbox();
            loadCombobox();
            btnAdd.setText("Save");
            tblProduct.setEnabled(false);
            btnUpdate.setEnabled(false);
            btnDelete.setText("Cancel");
        } else {
            try {
                TbEmployee emp = (TbEmployee) cateDao.getById(TbEmployee.class, 1);
                String name = txtName.getText();
                String detail = txtDetail.getText();
                double price = Double.parseDouble(txtPrice.getText());
                int quantity = Integer.parseInt(txtQuantity.getText());

                System.out.println("CATEID: " + vCateId.get(cboCategory.getSelectedIndex()).toString());
                TbCategory cate = (TbCategory) cateDao.getById(TbCategory.class, Integer.parseInt(vCateId.get(cboCategory.getSelectedIndex()).toString()));
                TbSupplier sup = (TbSupplier) supDao.getById(TbSupplier.class, Integer.parseInt(vSupId.get(cboSupplier.getSelectedIndex()).toString()));
                TbMeasure measure = (TbMeasure) measureDao.getById(TbMeasure.class, Integer.parseInt(vMeasureId.get(cboMeasure.getSelectedIndex()).toString()));


                TbProduct p = new TbProduct();
                p.setPname(name);
                p.setPdetail(detail);
                p.setPprice(price);
                p.setPquantity(quantity);
                p.setStatus(true);
                p.setTbEmployee(emp);
                p.setTbCategory(cate);
                p.setTbMeasure(measure);
                p.setTbSupplier(sup);


                productDao.insert(p);
                JOptionPane.showMessageDialog(null, "Insert success.");
            } catch (Exception e) {
                e.printStackTrace();
                JOptionPane.showMessageDialog(null, "Insert error.");
            }
            loadData();
            disableTextbox();
            Reset();
            btnAdd.setText("Add");
            tblProduct.setEnabled(true);
            btnUpdate.setEnabled(true);
            btnDelete.setText("Delete");
        }
    }//GEN-LAST:event_btnAddActionPerformed

    private void btnDeleteActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnDeleteActionPerformed
        // TODO add your handling code here:
        if (btnDelete.getText().equalsIgnoreCase("delete")) {
            if (tblProduct.getSelectedRow() == -1) {
                JOptionPane.showMessageDialog(null, "Select product to delete.");
                return;
            } else {
                int check = JOptionPane.showConfirmDialog(null, "Are you sure");
                if (check == JOptionPane.OK_OPTION) {
                    int rowIndex = tblProduct.getSelectedRow();
                    int productId = Integer.parseInt(tblProduct.getValueAt(rowIndex, 0).toString());
                    TbProduct p = (TbProduct) cateDao.getById(TbProduct.class, productId);
                    productDao.delete(p);
                    JOptionPane.showMessageDialog(null, "Delete success.");
                    loadData();
                }
            }
        } else {
            disableTextbox();
            tblProduct.setEnabled(true);
            btnAdd.setText("Add");
            btnAdd.setEnabled(true);
            btnUpdate.setText("Edit");
            btnUpdate.setEnabled(true);
            btnDelete.setText("Delete");
            Reset();
        }
    }//GEN-LAST:event_btnDeleteActionPerformed

    private void btnUpdateActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnUpdateActionPerformed
        // TODO add your handling code here:
        if (btnUpdate.getText().equalsIgnoreCase("edit")) {
            if (tblProduct.getSelectedRow() == -1) {
                JOptionPane.showMessageDialog(null, "Select product to edit.");
                return;
            } else {
                enableTextbox();
                binding();
                btnUpdate.setText("Save");
                tblProduct.setEnabled(false);
                btnAdd.setEnabled(false);
                btnDelete.setText("Cancel");
            }
        } else {
            int id = Integer.parseInt(txtId.getText());
            String name = txtName.getText();
            String detail = txtDetail.getText();
            int quantity = Integer.parseInt(txtQuantity.getText());
            double price = Double.parseDouble(txtPrice.getText());

            TbCategory cate = (TbCategory) cateDao.getById(TbCategory.class, Integer.parseInt(vCateId.get(cboCategory.getSelectedIndex()).toString()));
            TbSupplier sup = (TbSupplier) supDao.getById(TbSupplier.class, Integer.parseInt(vSupId.get(cboSupplier.getSelectedIndex()).toString()));
            TbMeasure measure = (TbMeasure) measureDao.getById(TbMeasure.class, Integer.parseInt(vMeasureId.get(cboMeasure.getSelectedIndex()).toString()));

            TbProduct p = (TbProduct) productDao.getById(TbProduct.class, id);

            p.setPname(name);
            p.setPdetail(detail);
            p.setPprice(price);
            p.setPquantity(quantity);
            p.setStatus(true);
            p.setTbCategory(cate);
            p.setTbSupplier(sup);
            p.setTbMeasure(measure);


            productDao.update(p);
            JOptionPane.showMessageDialog(null, "Update success.");
            loadData();
            disableTextbox();
            Reset();

            btnUpdate.setText("Edit");
            tblProduct.setEnabled(true);
            btnAdd.setEnabled(true);
            btnDelete.setText("Delete");
        }
    }//GEN-LAST:event_btnUpdateActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnAdd;
    private javax.swing.JButton btnDelete;
    private javax.swing.JButton btnUpdate;
    private javax.swing.JComboBox cboCategory;
    private javax.swing.JComboBox cboMeasure;
    private javax.swing.JComboBox cboSupplier;
    private javax.swing.JLabel jLabel12;
    private javax.swing.JLabel jLabel14;
    private javax.swing.JLabel jLabel18;
    private javax.swing.JLabel jLabel20;
    private javax.swing.JLabel jLabel22;
    private javax.swing.JLabel jLabel24;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JLabel lblCategory;
    private javax.swing.JLabel lblDescription;
    private javax.swing.JLabel lblId;
    private javax.swing.JLabel lblMeasure;
    private javax.swing.JLabel lblName;
    private javax.swing.JLabel lblPrice;
    private javax.swing.JLabel lblQuantity;
    private javax.swing.JLabel lblSupplier;
    private javax.swing.JTable tblProduct;
    private javax.swing.JTextField txtDetail;
    private javax.swing.JTextField txtId;
    private javax.swing.JTextField txtName;
    private javax.swing.JTextField txtPrice;
    private javax.swing.JTextField txtQuantity;
    // End of variables declaration//GEN-END:variables
}
